CONFIG_URL = 'http://bumik24.nazwa.pl/BBchecker/config.xml';

function showError(msg){
	$('#step3_error').html(msg).show();
	$('#step3_ok').hide();
	$('#step3').show();
}
function showBB(msg){
	$('#tablecol').html(msg);
	$('#step3_ok').show();
	$('#step3_error').hide();
	$('#step3').show();
}

function verify2(data){
	token = $('#token').val();

	//get field containing right token
	var tempTable = '';
	x = $(data).find("shortPreToken:contains('" + token + "')").get(0);
	
	if(x){
		//get parent element (ballot) and find row definition
		$(x).parent().find("row").each(function(){tempTable += '<tr><td>' + ($(this).attr('val') == 1 ? 'X' : '&nbsp;') + '</td></tr>'; });
		showBB(tempTable);
	}
	else{
		showError('Error! There is no column on Bulletin Board with that token.');
	}
	
	hideLoader();
}

function verify(data){
	token = $('#token').val();

	//look for good part of BB to download
	found = false;
	$(data).find('file').each(function(i, tmp){
		tmp = $(tmp);
		begin = tmp.attr('begin');
		end = tmp.attr('end');
		
		if(begin <= token && token <= end){
			//found!
			found = tmp.attr('name');
			return false;
		}

		i++;
	});
	
	if(found !== false){
		//download file and show column
		path = $(data).find('files').first().attr('path') + found;
		$.get(path, verify2);
	}
	else{
		hideLoader();
		showError('Error! There is no column on Bulletin Board with that token.');
	}
}

$('#btn_step2').click(function(){ showLoader('#step2'); $.get(CONFIG_URL, verify); });
hideLoader();